package tv.huan.web.controller.media;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tv.huan.common.annotation.Log;
import tv.huan.common.core.controller.BaseController;
import tv.huan.common.core.domain.AjaxResult;
import tv.huan.common.core.page.TableDataInfo;
import tv.huan.common.enums.BusinessType;
import tv.huan.common.utils.poi.ExcelUtil;
import tv.huan.media.domain.Authorizer;
import tv.huan.media.service.IAuthorizerService;

/**
 * 授权方Controller
 * 
 * @author admin
 * @date 2019-10-08
 */
@Controller
@RequestMapping("/media/authorizer")
public class AuthorizerController extends BaseController
{
    private String prefix = "media/authorizer";

    @Autowired
    private IAuthorizerService authorizerService;

    @RequiresPermissions("media:authorizer:view")
    @GetMapping()
    public String authorizer()
    {
        return prefix + "/authorizer";
    }

    /**
     * 查询授权方列表
     */
    @RequiresPermissions("media:authorizer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Authorizer authorizer)
    {
        startPage();
        List<Authorizer> list = authorizerService.selectAuthorizerList(authorizer);
        return getDataTable(list);
    }

    /**
     * 导出授权方列表
     */
    @RequiresPermissions("media:authorizer:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Authorizer authorizer)
    {
        List<Authorizer> list = authorizerService.selectAuthorizerList(authorizer);
        ExcelUtil<Authorizer> util = new ExcelUtil<Authorizer>(Authorizer.class);
        return util.exportExcel(list, "authorizer");
    }

    /**
     * 新增授权方
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存授权方
     */
    @RequiresPermissions("media:authorizer:add")
    @Log(title = "授权方", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Authorizer authorizer)
    {
        return toAjax(authorizerService.insertAuthorizer(authorizer));
    }

    /**
     * 修改授权方
     */
    @GetMapping("/edit/{authorizerId}")
    public String edit(@PathVariable("authorizerId") Long authorizerId, ModelMap mmap)
    {
        Authorizer authorizer = authorizerService.selectAuthorizerById(authorizerId);
        mmap.put("authorizer", authorizer);
        return prefix + "/edit";
    }

    /**
     * 修改保存授权方
     */
    @RequiresPermissions("media:authorizer:edit")
    @Log(title = "授权方", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Authorizer authorizer)
    {
        return toAjax(authorizerService.updateAuthorizer(authorizer));
    }

    /**
     * 删除授权方
     */
    @RequiresPermissions("media:authorizer:remove")
    @Log(title = "授权方", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try {
            authorizerService.deleteAuthorizerByIds(ids);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }
}
